﻿using System;
using System.Collections.Generic;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS.Web;
using IFRS_Global;

public partial class IFRS_WIN03_2E : PageBase
{
    #region === Event ===

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewMenu();
            BindControl();
            BindData();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCONFIRM_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN03_1S.aspx");
    }

    #endregion

    #region === Method ===

    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN03_2E;
    }
    private void BindControl()
    {
        btnSAVE.ValidationGroup = "Master";
        reqTransactionDate.SetRequiredField(lblTransactionDate, txtTransactionDate, btnSAVE);
        reqDataType.SetRequiredField(lblDataType, ddlDataType, btnSAVE);
        reqGLAccount.SetRequiredField(lblGLAccount, txtGLAccountCode, btnSAVE);
        reqCostCenter.SetRequiredField(lblCostCenter, txtCostCenter, btnSAVE);
        reqAmount.SetRequiredField(lblAmount, txtAmount, btnSAVE);
        reqSubsidyDeposit.SetRequiredField(lblSubsidyDeposit, txtSubsidyDeposit, btnSAVE);

        ddlDataType.SetData(DDL_TYPE.SubsidyDeposit, string.Empty, false, true);

        btnGLAccountCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount.aspx") + "','700','550');");
        txtGLAccountCode.AddControl(txtGLAccountCode.ClientID);
        txtGLAccountCode.ControlSearch = txtGLAccountCode.ClientID;
        txtGLAccountCode.SearchKey = "GLAccount_oGLAccountCode_oGLAccountName";

        btnCostCenter.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/CostCenter.aspx") + "','700','550');");
        txtCostCenter.AddControl(hidCostCenter.ClientID);
        txtCostCenter.AddControl(txtCostCenter.ClientID);
        txtCostCenter.ControlSearch = txtCostCenter.ClientID;
        txtCostCenter.SearchKey = "VWCostCenter_oCOST_CENTER_CD_oCOST_CENTER_NM";

        txtStatus.Text = Resources.ifrsField.CREATE;
    }
    private void BindData()
    {
        if (Convert.ToString(Request.QueryString["id"]) != string.Empty)
        {

            var id = Convert.ToInt32(Request.QueryString["id"]);
            var dt = new SPR_SUBSIDYDEPOSIT().SEARCH_BYID(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                var list = new List<SubsidyDeposit>();
                ConvertHelper.ConvertDataTableToObject(dt, list);

                txtTransactionDate.Text = list[0].TransactionDate.DateToString();

                ddlDataType.SelectedValue = list[0].DataTypeID.IntToString();
                txtGLAccountCode.Text = list[0].GLAccount;
                hidCostCenter.Value = list[0].CostCenter;
                txtCostCenter.Text = list[0].COST_CENTER_NM;
                txtAmount.Text = list[0].Amount.DecimalToString();
                txtSubsidyDeposit.Text = list[0].SubsidyDepositValue.DecimalToString();

                txtStatus.Text = list[0].IsConfirmed == true ? Resources.ifrsField.CONFIRM : Resources.ifrsField.CREATE;

                if (list[0].IsConfirmed != true) return;
                JS_Alert(Resources.ifrsResource.Confirmed, ResolveUrl(string.Format("~/IFRS/WIN03_3V.aspx?&id={0}", list[0].SubsidyDepositID)));
            }
        }
    }
    private void SaveData(bool isConfirmed = false)
    {
        if (!Page.IsValid) return;

        var dal = new SPR_SUBSIDYDEPOSIT();
        var models = new SubsidyDeposit();

        models.SubsidyDepositID = Convert.ToInt32(Request.QueryString["id"]);

        models.DataTypeID = ddlDataType.SelectedValue.AsInt32();
        models.TransactionDate = txtTransactionDate.Text.StringToDateAllowNull();
        models.GLAccount = txtGLAccountCode.Text;
        models.CostCenter = hidCostCenter.Value;
        models.Amount = txtAmount.Text.ParseDecimal();
        models.SubsidyDepositValue = txtSubsidyDeposit.Text.ParseDecimal();

        models.IsConfirmed = isConfirmed;
        models.RP_GSB_PRDCD = DefaultValue.RP_GSB_PRDCD.SSDY_DEPO;

        if (ddlDataType.SelectedValue == IFRS_Global.DataType.InterestPayment) // การชำระดอกเบี้ย
            models.RP_MAPPING_CD = DefaultValue.RP_MAPPING_CD.SSDY_DEPO_INT;
        if (ddlDataType.SelectedValue == IFRS_Global.DataType.UnpaidInterest) // ดอกเบี้ยค้างชำระ
            models.RP_MAPPING_CD = DefaultValue.RP_MAPPING_CD.SSDY_DEPO_ACUINT;

        models.RP_SOURCE_FILE = DefaultValue.RP_SOURCE_FILE.MANUAL_SSDY_DEPO;

        models.SetStandardField();

        var result = dal.UPDATE(models);
        if (result > 0)
        {
            if (isConfirmed)
                JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN03_3V.aspx?&id={0}", models.SubsidyDepositID)));
            else
                JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN03_2E.aspx?&id={0}", models.SubsidyDepositID)));
                
        }
        else // Duplicate
            Utility.AlertMessage(Page,result == -1 ? Resources.ifrsResource.Duplicate : Resources.ifrsResource.CanNotSaveData);
    }

    #endregion
}